package ch.il07.utils;

import java.util.Random;

/**
 * 
 * @author Reto Hablützel
 * 
 * @desc generates random strings
 *
 */
public final class RandomHelper {
	
	private static Random gen = new Random();
	
	/**
	 * only static access
	 */
	private RandomHelper(){}
	
	public static String randString(){
		return randString(10);
	}

	/**
	 * 
	 * @param size
	 * 
	 * @return get a random string with the specified size
	 * 
	 * @throws ToStupidToUseThisException if size is negative
	 */
	public static String randString(int size){
		if(size <= 0){
			throw new ToStupidToUseThisException();
		}
		char[] rand = new char[size];
		for (int i = 0; i < rand.length; i++) {
			rand[i] = (char) getNumber(97, 122);
		}
		return new String(rand);
	}
	
	private static int getNumber(int min, int max){
		if(min >= max){
			throw new ToStupidToUseThisException();
		}
		int rand;
		while((rand = gen.nextInt(max)) < min)
			; // ignore
		return rand;
	}

	/**
	 * 
	 * @desc creates a pseudo email address like username@username.com
	 * 
	 * @param username
	 * 
	 * @return the email address
	 */
	public static String getEmail(String username) {
		return username + "@" + username +".com";
	}
}

class ToStupidToUseThisException extends RuntimeException{

	private static final long serialVersionUID = -7086577015232069417L;
	
}
